import { defineStore } from 'pinia'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

export default defineStore('inpatient-doctor-batch-temperature', {
  state: () => {
    return {
      searchData: {
        zjm: '',
        wardId: null,
        recordDate: dayjs().format('YYYY-MM-DD'),
        time: 2
      },
      tabActiveKey: 0,
      tableData: [],
      loading: false,
      isAuditStatus: true // 判断审核状态
    }
  },
  actions: {
    // 获取数据
    async getData() {
      this.tableData = []
      this.loading = true
      const { code, data, msg } = await post('/inp/zyhs/batchGetInpTpRecordResponseList', this.searchData)
      if (code !== 0) return message.error(msg || '获取数据失败')
      this.loading = false
      if (this.tabActiveKey === 1) {
        data.forEach((item) => {
          item.review = item.review === 1 ? true : false
        })
      }
      this.tableData = data
      this.isAuditStatus = data.every((item) => item.auditStatus === 0)
    },
    // 保存所有
    onSaveAll() {
      this.tableData.forEach(async (item, index) => {
        const params = {
          ...item,
          review: item.review ? 1 : 0,
          recordDate: this.searchData.recordDate,
          ...(this.tabActiveKey === 1 ? { time: this.searchData.time } : {})
        }
        await post('/inp/zyhs/doSaveInpTpRecord', params)
        if (index === this.tableData.length - 1) {
          message.success('保存成功')
          setTimeout(() => this.getData(), 300)
        }
      })
    },
    // 保存
    async onSave(row) {
      if (this.tabActiveKey === 1) {
        row.time = this.searchData.time
      }
      const { code, msg } = await post('/inp/zyhs/doSaveInpTpRecord', { ...row, review: row.review ? 1 : 0 })
      if (code !== 0) return message.error(msg || '保存失败')
      message.success('保存成功')
      this.getData()
    },
    // 审核&撤审
    async onClickExamine(status) {
      const { code, msg } = await post('/inp/zyhs/doModifyInpTpRecord', {
        batchIds: this.tableData.map((item) => item.id),
        auditStatus: status
      })
      if (code !== 0) return message.error(msg || '操作失败')
      message.success(msg)
      this.getData()
    },
    // 切换时间
    onClickDay(type) {
      if (type === '1') {
        this.searchData.recordDate = dayjs(this.searchData.recordDate).add(1, 'day').format('YYYY-MM-DD')
      } else {
        this.searchData.recordDate = dayjs(this.searchData.recordDate).subtract(1, 'day').format('YYYY-MM-DD')
      }
      this.getData()
    },
    // 打印记录单
    async onPrintRecord() {
      if (this.tabActiveKey === 1) {
        const { code, msg, data } = await post('/inp/Print/printTWDSD', this.searchData)
        if (code !== 0) return message.error(msg)
        window.openExe?.rpt(data) //调用winform打印
      } else {
        const { code, msg, data } = await post('/inp/Print/printTWDJC', this.searchData)
        if (code !== 0) return message.error(msg)
        window.openExe?.rpt(data) //调用winform打印
      }
    },
    // 重置
    async onReset() {
      this.searchData = {
        zjm: '',
        recordDate: dayjs().format('YYYY-MM-DD'),
        time: 2
      }
      this.tabActiveKey = 0
      await this.getData()
    }
  }
})
